home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / skey / src / skeysubr.c < prev    next >
C/C++ Source or Header  |  1993-11-08  |  5KB  |  320 lines

  1. /* S/KEY v1.1b (skeysubr.c)
  2.  *
  3.  * Authors:
  4.  *          Neil M. Haller <nmh@thumper.bellcore.com>
  5.  *          Philip R. Karn <karn@chicago.qualcomm.com>
  6.  *          John S. Walden <jsw@thumper.bellcore.com>
  7.  *
  8.  * Modifications: 
  9.  *          Scott Chasin <chasin@crimelab.com>
  10.  *
  11.  * S/KEY misc routines.
  12.  */
  13.  
  14. #include <stdio.h>
  15.  
  16. #ifdef HAS_STD_LIB
  17. #include <stdlib.h>
  18. #else
  19. #include <sys/types.h>
  20. #endif
  21.  
  22. #include <string.h>
  23. #include <signal.h>
  24.  
  25. #ifdef    __MSDOS__
  26. #include <dos.h>
  27. #endif
  28.  
  29. #ifdef stty
  30. # undef stty
  31. #endif
  32.  
  33. #ifdef gtty
  34. # undef gtty
  35. #endif
  36.  
  37. #ifndef SYSV
  38. # include <sgtty.h>
  39. # define TTYSTRUCT sgttyb
  40. # define stty(fd,buf) ioctl((fd),TIOCSETN,(buf))
  41. # define gtty(fd,buf) ioctl((fd),TIOCGETP,(buf))
  42. #else
  43. # include <termio.h>
  44. # define TTYSTRUCT termio
  45. # define stty(fd,buf) ioctl((fd),TCSETA,(buf))
  46. # define gtty(fd,buf) ioctl((fd),TCGETA,(buf))
  47. #endif
  48.  
  49. #ifdef SYSV
  50.     struct termio newtty;
  51.     struct termio oldtty;
  52. #else
  53.     struct sgttyb newtty;
  54.     struct sgttyb oldtty;
  55.     struct tchars chars;
  56. #endif
  57.  
  58. #ifdef SIGVOID
  59. #define SIGTYPE void
  60. #else
  61. #define SIGTYPE void
  62. #endif
  63.  
  64. SIGTYPE trapped();
  65.  
  66. #include "md4.h"
  67. #include "skey.h"
  68.  
  69. #if (defined(__MSDOS__) || defined(MPU8086) || defined(MPU8080) \
  70.     || defined(vax) || defined (MIPSEL))
  71. #define    LITTLE_ENDIAN
  72. #endif
  73.  
  74. /* Crunch a key:
  75.  * concatenate the seed and the password, run through MD4 and
  76.  * collapse to 64 bits. This is defined as the user's starting key.
  77.  */
  78. int
  79. keycrunch(result,seed,passwd)
  80. char *result;    /* 8-byte result */
  81. char *seed;    /* Seed, any length */
  82. char *passwd;    /* Password, any length */
  83. {
  84.     char *buf;
  85.     MDstruct md;
  86.     unsigned int buflen;
  87. #ifndef    LITTLE_ENDIAN
  88.     int i;
  89.     register long tmp;
  90. #endif
  91.     
  92.     buflen = strlen(seed) + strlen(passwd);
  93.     if ((buf = (char *)malloc(buflen+1)) == NULL)
  94.         return -1;
  95.     strcpy(buf,seed);
  96.     strcat(buf,passwd);
  97.  
  98.     /* Crunch the key through MD4 */
  99.     sevenbit(buf);
  100.     MDbegin(&md);
  101.     MDupdate(&md,(unsigned char *)buf,8*buflen);
  102.  
  103.     free(buf);
  104.  
  105.     /* Fold result from 128 to 64 bits */
  106.     md.buffer[0] ^= md.buffer[2];
  107.     md.buffer[1] ^= md.buffer[3];
  108.  
  109. #ifdef    LITTLE_ENDIAN
  110.     /* Only works on byte-addressed little-endian machines!! */
  111.     memcpy(result,(char *)md.buffer,8);
  112. #else
  113.     /* Default (but slow) code that will convert to
  114.      * little-endian byte ordering on any machine
  115.      */
  116.     for (i=0; i<2; i++) {
  117.         tmp = md.buffer[i];
  118.         *result++ = tmp;
  119.         tmp >>= 8;
  120.         *result++ = tmp;
  121.         tmp >>= 8;
  122.         *result++ = tmp;
  123.         tmp >>= 8;
  124.         *result++ = tmp;
  125.     }
  126. #endif
  127.  
  128.     return 0;
  129. }
  130.  
  131. /* The one-way function f(). Takes 8 bytes and returns 8 bytes in place */
  132. void f (x)
  133. char *x;
  134. {
  135.     MDstruct md;
  136. #ifndef    LITTLE_ENDIAN
  137.     register long tmp;
  138. #endif
  139.  
  140.     MDbegin(&md);
  141.     MDupdate(&md,(unsigned char *)x,64);
  142.  
  143.     /* Fold 128 to 64 bits */
  144.     md.buffer[0] ^= md.buffer[2];
  145.     md.buffer[1] ^= md.buffer[3];
  146.  
  147. #ifdef    LITTLE_ENDIAN
  148.     /* Only works on byte-addressed little-endian machines!! */
  149.     memcpy(x,(char *)md.buffer,8);
  150.  
  151. #else
  152.     /* Default (but slow) code that will convert to
  153.      * little-endian byte ordering on any machine
  154.      */
  155.     tmp = md.buffer[0];
  156.     *x++ = tmp;
  157.     tmp >>= 8;
  158.     *x++ = tmp;
  159.     tmp >>= 8;
  160.     *x++ = tmp;
  161.     tmp >>= 8;
  162.     *x++ = tmp;
  163.  
  164.     tmp = md.buffer[1];
  165.     *x++ = tmp;
  166.     tmp >>= 8;
  167.     *x++ = tmp;
  168.     tmp >>= 8;
  169.     *x++ = tmp;
  170.     tmp >>= 8;
  171.     *x = tmp;
  172. #endif
  173. }
  174.  
  175. /* Strip trailing cr/lf from a line of text */
  176. void rip (buf)
  177. char *buf;
  178. {
  179.     char *cp;
  180.  
  181.     if((cp = strchr(buf,'\r')) != NULL)
  182.         *cp = '\0';
  183.  
  184.     if((cp = strchr(buf,'\n')) != NULL)
  185.         *cp = '\0';
  186. }
  187.  
  188. #ifdef    __MSDOS__
  189. char *readpass(buf,n)
  190. char *buf;
  191. int n;
  192. {
  193.   int i;
  194.   char *cp;
  195.  
  196.   for (cp=buf,i = 0; i < n ; i++)
  197.        if ((*cp++ = bdos(7,0,0)) == '\r')
  198.           break;
  199.    *cp = '\0';
  200.    putchar('\n');
  201.    rip(buf);
  202.    return buf;
  203. }
  204. #else
  205.  
  206. char *readpass (buf,n)
  207. char *buf;
  208. int n;
  209. {
  210.  
  211. #ifndef USE_ECHO
  212.     set_term ();
  213.     echo_off ();
  214. #endif
  215.  
  216.     fgets (buf, n, stdin);
  217.  
  218.     rip (buf);
  219.  
  220.     printf ("\n\n");
  221.     sevenbit (buf);
  222.  
  223. #ifndef USE_ECHO
  224.     unset_term ();
  225. #endif
  226.     return buf;
  227. }
  228.  
  229. set_term () 
  230. {
  231.     gtty (fileno(stdin), &newtty);
  232.     gtty (fileno(stdin), &oldtty);
  233.  
  234.     signal (SIGINT, trapped);
  235. }
  236.  
  237. echo_off ()
  238. {
  239.  
  240. #ifdef SYSV
  241.     newtty.c_lflag &= ~(ICANON | ECHO | ECHONL);
  242. #else
  243.     newtty.sg_flags |= CBREAK;
  244.     newtty.sg_flags &= ~ECHO;
  245. #endif
  246.  
  247. #ifdef SYSV
  248.     newtty.c_cc[VMIN] = 1;
  249.     newtty.c_cc[VTIME] = 0;
  250.     newtty.c_cc[VINTR] = 3;
  251. #else
  252.     ioctl(fileno(stdin), TIOCGETC, &chars);
  253.     chars.t_intrc = 3;
  254.     ioctl(fileno(stdin), TIOCSETC, &chars);
  255. #endif
  256.  
  257.     stty (fileno (stdin), &newtty);
  258. }
  259.  
  260. unset_term ()
  261. {
  262.     stty (fileno (stdin), &oldtty);
  263.  
  264. #ifndef SYSV
  265.     ioctl(fileno(stdin), TIOCSETC, &chars);
  266. #endif
  267. }
  268.  
  269. void trapped()
  270.  {
  271.   signal (SIGINT, trapped);
  272.   printf ("^C\n");
  273.   unset_term ();
  274.   exit (-1);
  275.  }
  276.  
  277. #endif
  278.  
  279. /* removebackspaced over charaters from the string */
  280. backspace(buf)
  281. char *buf;
  282. {
  283.     char bs = 0x8;
  284.     char *cp = buf;
  285.     char *out = buf;
  286.  
  287.     while(*cp){
  288.         if( *cp == bs ) {
  289.             if(out == buf){
  290.                 cp++;
  291.                 continue;
  292.             }
  293.             else {
  294.               cp++;
  295.               out--;
  296.             }
  297.         }
  298.         else {
  299.             *out++ = *cp++;
  300.         }
  301.  
  302.     }
  303.     *out = '\0';
  304.     
  305. }
  306.  
  307. /* sevenbit ()
  308.  *
  309.  * Make sure line is all seven bits.
  310.  */
  311.  
  312. sevenbit (s)
  313. char *s;
  314. {
  315.    while (*s) {
  316.      *s = 0x7f & ( *s);
  317.      s++;
  318.    }
  319. }
  320.